import 'package:extended_text_library/extended_text_library.dart';
import 'package:flutter/material.dart';
import 'package:osc/common/MyTextStyle.dart';
import 'package:osc/utils/emoji_util.dart';
import 'package:osc/utils/pd_log.dart';
import 'package:osc/utils/regex_util.dart';
import 'package:osc/utils/special_text/common_href_text.dart';
import 'package:osc/utils/special_text/tweet_origin_author_text.dart';

import 'at_text.dart';
import 'emoji_text.dart';
import 'special_text_bean.dart';
import 'topic_text.dart';

class MySpecialTextSpanBuilder extends SpecialTextSpanBuilder {
  /// whether show background for @somebody
  final bool showAtBackground;
  final bool simpleAtTopicEnable;
  MySpecialTextSpanBuilder({this.showAtBackground: false, this.simpleAtTopicEnable: false});


  final String startFlagReg = "<" + ".*" + "[^<>]*?\\s?>(.*?)";
  String endFlag = "</"+"a>";

  String inputData;

  @override
  TextSpan build(String data, {TextStyle textStyle, onTap}) {
    inputData = data;
    var textSpan = super.build(data, textStyle: textStyle, onTap: onTap);
    return textSpan;
  }

  @override
  SpecialText createSpecialText(String flag,
      {TextStyle textStyle, SpecialTextGestureTapCallback onTap, int index}) {
    if (flag == null || flag == "") return null;

    ///index is end index of start flag, so text start index should be index-(flag.length-1)

    if(RegexUtil.isMatch(startFlagReg, flag)){
      try{
        String startFlag = RegexUtil.getMatches("<a[^<>]*?>", flag)[0];

        int type = SpecialTextBean.getType(flag);
        if(type == SpecialTextBean.TYPE_NONE){
          return null;
        }
        return CommonHrefText(
          type: type,
          beginFlag: startFlag,
          stopFlag: endFlag,
          textStyle: MyTextStyle.TWEET_CONTENT_SPECIAL_TEXT,
          onTap: onTap,
          start: index - (startFlag.length -1),
          showAtBackground: showAtBackground,
        );
      } on Error catch(err){
        PdLog.printLog("----$err");
        return null;
      }

    }
    else if(simpleAtTopicEnable && isStart(flag, AtText.flag)){
      return AtText(MyTextStyle.TWEET_CONTENT_SPECIAL_TEXT, onTap, start: index - (AtText.flag.length - 1));
    } else if(simpleAtTopicEnable && isStart(flag, TopicText.flag)){
      return TopicText(MyTextStyle.TWEET_CONTENT_SPECIAL_TEXT, onTap, start: index - (TopicText.flag.length - 1));
    }
    else if (isStart(flag, EmojiText.flagBracketsL)) {
      return EmojiText(textStyle: textStyle, startFlag: EmojiText.flagBracketsL, endFlag: EmojiText.flagBracketsR, start: index - (EmojiText.flagBracketsL.length - 1));
    } else if (!RegexUtil.isURLPrefix(flag) && isStart(flag, EmojiText.flagColon)) {
      String tempData = inputData.substring(index+1);
      int endFlagIndex = tempData.indexOf(EmojiText.flagColon);
      if(endFlagIndex >= 0){
        String emojiName = inputData.substring(index+1, index+endFlagIndex+1);
        if(EmojiUitl.instance.emojiMap.containsValue(emojiName)) {
          return EmojiText(textStyle: textStyle,
              startFlag: EmojiText.flagColon,
              endFlag: EmojiText.flagColon,
              start: index - (EmojiText.flagBracketsL.length - 1));
        } else {
          return null;
        }
      } else {
        return null;
      }
    } else if(isStart(flag, TweetOriginAuthorText.beginFlag)){
      return TweetOriginAuthorText(MyTextStyle.TWEET_CONTENT_SPECIAL_TEXT, onTap, start: index - (TweetOriginAuthorText.beginFlag.length - 1));
    }
    return null;
  }
}
